#! /usr/bin/python
"""
Provides methods that can be used to crack vaults secured with passwords
that can be found in the dictionary.

Along with the dictionary words there are some other words included that
are common passwords.
"""
import sys, os
sys.path.insert(0, "..")
import time
from Vault import Vault

def crack(vault):
    """
    Attempts to break into a vault using a dictionary attack
    Times the attempt and returns the guessed value and time taken
    
    Usage: (guess, data, time) = crack(vault)
    """
    tick = time.time()
    data = None
    dictionary = open("../commonpasswords.txt")
    for guess in dictionary:
        try:
            data = vault.open(guess.strip())
            dictionary.close()
            break
        except:
            pass
    if data == None:
        raise Exception("Could not crack password:")
    tock = time.time()
    return (guess, data, (tock - tick))
        
if __name__ == "__main__":
    v = Vault("password123", "This is super secret information")
    (password, data, cracktime) = crack(v)
    print "I got it! Your key was %s" % password.strip()
    print "The vault contained:"
    print "    %s" % data
    print "It took %f seconds to break" % cracktime
